package com.yanftch.stopcoding.utils

import java.awt.image.BufferedImage
import javax.swing.ImageIcon
import javax.swing.JLabel
import javax.swing.JPanel
import javax.imageio.ImageIO

object ImageConfig {
    fun loadImage(): BufferedImage? {
        // 获取资源文件的输入流
        val resourceStream = this.javaClass.getResourceAsStream("/files/image_01.jpeg")
        return try {
            // 将输入流转换为 BufferedImage
            val image = ImageIO.read(resourceStream)
            image // 直接返回 BufferedImage
        } catch (e: Exception) {
            e.printStackTrace()
            null // 如果发生异常，返回 null
        } finally {
            resourceStream?.close() // 关闭输入流
        }
    }


    @JvmStatic
    fun loadImageIcon(): ImageIcon {
        // 获取资源文件的输入流
        val resourceStream = this.javaClass.getResourceAsStream("/files/image_01.jpeg")
        return try {
            // 将输入流转换为 BufferedImage
            val image = ImageIO.read(resourceStream)
            // 创建 ImageIcon
            ImageIcon(image)
        } catch (e: Exception) {
            e.printStackTrace()
            ImageIcon()
        } finally {
            resourceStream?.close() // 关闭输入流
        }
    }

    // 在某个方法中使用 loadImageIcon()
    fun createImagePanel(): JPanel {
        val panel = JPanel()
        val imageIcon = loadImageIcon()

        if (imageIcon != null) {
            // 将 ImageIcon 显示在 JLabel 中
            val label = JLabel(imageIcon)
            panel.add(label)
        }

        return panel
    }

}